<?php
/**
 * Object Relational Persistence Mapping Class for table: lab_service
 * @package modules.dept_laboratory.ordo
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class LabService extends ORDataObject {

	/**#@+
	 * Fields of table: lab_service mapped to class members
	 */
	var $lab_service_id		= '';
	var $lab_group_id		= '';
	var $service_id		= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'lab_service';

	/**
	 * Primary Key
	 */
	var $_key = 'lab_service_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'LabService';

	/**
	 * Handle instantiation
	 */
	function __construct() {
		parent::ORDataObject();
	}
	/**
	 * Get list of lab services of given lab group id.
	 * @param $groupId
	 * @return Array(service_id,name)
	 */
	public function loadLabServices($groupId) {
		$field    =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        
        $ds =& new Datasource_sql();
        $sql = array(
            'cols'  => "t.service_id, s.$field as name",
            'from'  => "$this->_table t 
                    INNER JOIN service s ON (t.service_id=s.service_id AND s.disabled='0' AND s.item_type='laboratory')  
                    ",  
            'where' => " t.lab_group_id='$groupId'",          
            'orderby' => "name"
            );
        $ds->setup($this->_db, $sql);
        return $ds->toArray();  
	}
	
    /**
     * Get list of lab services of given lab group id.
     * @param $groupId
     * @return Array(service_id,name)
     */
    public function loadLabServices4Admin($groupId) {
        $field    =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        
        $ds =& new Datasource_sql();
        $sql = array(
            'cols'  => "t.service_id, s.$field as name, s.disabled, s.price",
            'from'  => "$this->_table t 
                INNER JOIN service s ON (t.service_id=s.service_id AND s.item_type='laboratory')  
                ",  
            'where' => " t.lab_group_id='$groupId'",          
            'orderby' => "name, s.disabled"
            );
        $ds->setup($this->_db, $sql);
        return $ds->toArray();  
    }

    /**
     * 
     * @param $serviceCode
     * @return unknown_type
     */
    public function getLabAutoCompleteByCode($serviceCode) {
        $field    =   L10nHelper::changeField(L10nHelper::getLocale(), "name");
        $ds       =   new Datasource_sql();       
        $sql = array (
            'cols'  => "s.service_id, s.code, s.$field service_name, g.lab_group_id, g.$field lab_group_name",
            'from'      => "service s
                        INNER JOIN lab_service l ON s.service_id = l.service_id
                        INNER JOIN lab_group g ON l.lab_group_id = g.lab_group_id",
            'where'     => "s.code LIKE '%$serviceCode%'",
            'orderby'   => "s.code"
            );
        $ds->setup($this->_db, $sql);
        return $ds->toArray();
    }
    
    public function getLabAutoCompleteByName($serviceName) {
        $field    =   L10nHelper::changeField(L10nHelper::getLocale(), "name");
        $ds       =   new Datasource_sql();       
        $sql = array (
            'cols'  => "s.service_id, s.code, s.$field service_name, g.lab_group_id, g.$field lab_group_name",
            'from'      => "service s
                        INNER JOIN lab_service l ON s.service_id = l.service_id
                        INNER JOIN lab_group g ON l.lab_group_id = g.lab_group_id",
            'where'     => "s.$field LIKE '%$serviceName%'",
            'orderby'   => "service_name"
            );
        $ds->setup($this->_db, $sql);
        return $ds->toArray();
    }
}
?>
